//
//  SendHttpOperation.m
//  YOGProj
//
//  Created by liyun on 14-6-27.
//  Copyright (c) 2014年 liyun. All rights reserved.
//

#import "HCSendHttpOperation.h"
#import "ASIFormDataRequest.h"
#import "HCFileOperation.h"
#import "HCMacroDefinition.h"
#import "HCPublicStringConvert.h"

static HCSendHttpOperation *instance = nil;

//static NSString *url_base = @"http://192.168.0.202:8088/OYPlatform/app/";
//static NSString *url_base = @"http://51agile.xicp.net:8088/OYPlatform/app/";
static NSString *url_base = @"http://180.96.38.221:8080/OYPlatform/app/";



@implementation HCSendHttpOperation

@synthesize newsDelegate;

/************************************************************************************************
 函数名称 : + (SendHttpOperation *)shareSendHttpOperation
 函数描述 : 创建单例，在其他文件中可以直接类名加方法调用该类中方法
 输入参数 : 无
 返回    : 无
 备注    : 无
 ************************************************************************************************/
+ (HCSendHttpOperation *)shareSendHttpRequest
{
    if(!instance)
    {
        instance = [[HCSendHttpOperation alloc] init];
    }
    return instance;
}

//获取青奥在线数据
- (void)getNews:(int)type       //type      新闻类型 1赛事,2任务,3官方,4活动;无参数为 综合;图文-1
         pageNo:(int)pageNo     //pageNo    第几页
       pageSize:(int)pageSize   //pageSize  每页多少条记录
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"HCLOAD" object:nil];
    
//    type = type-2;
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSString *urlStr;
        urlStr = [NSString stringWithFormat:@"%@picturenews?language=%@&pageSize=%d&pageNo=%d&type=%d",url_base,NSLocalizedString(@"HClanguage", @""),pageSize,pageNo,type];
        
        NSLog(@"urlStr-->%@",urlStr);
        ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlStr]];
        [request setDelegate:self];
        [request setDidFinishSelector:@selector(urlRequestFinished:)];
        [request setDidFailSelector:@selector(urlRequestFail:)];
        [request startAsynchronous];
    });
}

-(void)urlRequestFail:(ASIHTTPRequest *)request
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"HCREMOVE" object:nil];
    NSError *error;
    error=request.error;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"HCTIP" object:@{@"HCmsg":NSLocalizedString(@"HC网络连接失败,请重新尝试", @"")}];
}

-(void)urlRequestFinished:(ASIHTTPRequest *)request
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"HCREMOVE" object:nil];
    NSData *data=[request responseData];
    NSDictionary *dic = [NSDictionary dictionaryWithDictionary:[[HCPublicStringConvert sharePublicStringConvert] convertDic:data]];

    [newsDelegate passNew:dic];
}


#pragma mark 
-(void)getDataWithUUID:(NSString *)uuid
{
    NSURL *urlWithUUID = [[NSURL alloc]initWithString:[NSString stringWithFormat:@"%@apprequest?uuid=%@",url_base,uuid]];
    NSLog(@"uuid-->%@",urlWithUUID);
    
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:urlWithUUID];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(urlRequestFinished2:)];
    [request setDidFailSelector:@selector(urlRequestFail2:)];
    request.userInfo = [[NSDictionary alloc]initWithObjectsAndKeys:uuid,@"uuid", nil];
    [request startAsynchronous];
}

-(void)urlRequestFinished2:(ASIHTTPRequest *)request
{
    
    NSData       *data = [request responseData];
    NSDictionary *dic  = [NSDictionary dictionaryWithDictionary:[[HCPublicStringConvert sharePublicStringConvert] convertDic:data]];
    
//    //    NSLog(@"dic-->%@",dic);
    if ([dic[@"success"] intValue] == 1)
    {
        NSLog(@"response-->%@",request.responseString);
        
        NSDate          *now       = [NSDate date];
        NSDateFormatter *formatter = [[NSDateFormatter  alloc ]  init];
        [formatter      setDateFormat:@"yyyy-MM-dd HH:mm"];
        NSString        *nowTime   = [formatter stringFromDate:now];
        
//        NSString        *UUIDTypeList    = [[NSBundle mainBundle]pathForResource:@"UUIDTypeList" ofType:@"plist"];
//        NSDictionary    *typeForUUID_Dic = [[NSDictionary alloc]initWithContentsOfFile:UUIDTypeList];
        NSDictionary    *typeForUUID_Dic = [[NSDictionary alloc]initWithContentsOfFile:[BUNDLE pathForResource:@"HCUUIDTypeList" ofType:@"plist"]];
        int             typeForUUID      = [typeForUUID_Dic[request.userInfo[@"uuid"]] intValue];
        
        NSLog(@"typeForUUID-->%d",typeForUUID);
        switch (typeForUUID)
        {
            case 1:
            {
                //餐厅
                [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"HCSHOW1"];
                [[HCFileOperation shareFileOperation] saveSearchHistory:@{nowTime:dic[@"retData"][@"foods"]} type:1];
            }
                break;
                
            case 2:
            {
                //宗教服务
                [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"HCSHOW2"];
                [[HCFileOperation shareFileOperation] saveSearchHistory:@{nowTime:dic[@"retData"]} type:2];
            }
                break;
                
            case 3:
            {
                 //健身训练服务
                [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"HCSHOW3"];
                [[HCFileOperation shareFileOperation] saveSearchHistory:@{nowTime:dic[@"retData"]} type:3];
            }
                break;
            case 4:
            {
                //青奥村广场
                [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"HCSHOW4"];
                [[HCFileOperation shareFileOperation] saveSearchHistory:@{nowTime:dic[@"retData"]} type:4];
            }
                break;
            default:
                break;
        }
        [[NSNotificationCenter defaultCenter]postNotificationName:@"HCrefreshTableView"
                                                           object:@{@"HCtype":[NSString stringWithFormat:@"%d",typeForUUID]}];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"HCVillage" object:nil];
    }
    else
    {
        NSLog(@"false");
        [self performSelector:@selector(retryGetData:) withObject:request afterDelay:20];
    }
}


-(void)urlRequestFail2:(ASIHTTPRequest *)request
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"HCTIP"
                                                        object:@{@"HCmsg":NSLocalizedString(@"HC网络连接失败,请重新尝试", @"")}];
    
    [self performSelector:@selector(retryGetData:) withObject:request afterDelay:20];
}

- (void)retryGetData:(ASIHTTPRequest *)request
{
    NSLog(@"PPPOOO");
    [self getDataWithUUID:request.userInfo[@"uuid"]];

}





@end
